import Vue from 'vue'
import store from '../../store'
import XThirdLogin from '../../x/XThirdLogin'

export default {
    userAuth(to, next) {
        //用户信息拦截
        let userInfo = store.getters.currentUserFn
        if(userInfo){
            store.dispatch('updateCurrentUser', userInfo)
        }
        if (to.meta.userAuth) {
            if (userInfo == null || userInfo == 'undefined' || userInfo == 'null' || !userInfo || !userInfo.userId) {
                next({
                    path: '/to/login'
                })
                return true;
            }
        }
        return false;
    },
    thirdAuth(to, next) {
        //第三方登录拦截
        let thirdInfo = store.getters.currentThirdFn;
        if(thirdInfo){
            store.commit('updateCurrentThird', thirdInfo)
        }
        if (to.meta.thirdAuth) {
            if (thirdInfo == null || thirdInfo == 'undefined' || thirdInfo == 'null'
                || thirdInfo.thirdId == null || thirdInfo.thirdId == 'undefined' || thirdInfo.thirdId == 'null'
                || thirdInfo.thirdId == 0 || !thirdInfo.thirdId) { //即无第三方登录id
                if(XThirdLogin.toLogin()){
                    store.state.isThirdLogin = true;
                    next()
                }
                return true
            }
        }
        return false
    },
    userOrThird(to, next) {
        if (to.meta.userOrThird) {
            if ((userInfo == null || userInfo == 'undefined' || userInfo == 'null') ||
                (userInfo.thirdId == null || userInfo.thirdId == 'undefined' || userInfo.thirdId == 'null' || userInfo.thirdId == 0)) {
                next({
                    path: '/to/login'
                })
                return true
            }
        }
        return false
    },
}
